package cn.bkrc.util;

import cn.bkrc.pojo.ResultMessage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

//把服务器端的数据转换成json格式的字符串
public class MessageUtils {

    public static String getMessage(boolean isSystemMessage,String fromName,Object message){
        try {
            ResultMessage resultMessage=new ResultMessage();
            resultMessage.setIsSystem(isSystemMessage);
            resultMessage.setMessage(message);
            if(fromName!=null){
                resultMessage.setFromName(fromName);
            }
            //转换为json格式
            ObjectMapper mapper=new ObjectMapper();
            return mapper.writeValueAsString(resultMessage);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
